package com.videoaggre.zcl.ui.activity

import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import com.kk.taurus.playerbase.assist.InterEvent
import com.kk.taurus.playerbase.assist.OnVideoViewEventHandler
import com.kk.taurus.playerbase.entity.DataSource
import com.kk.taurus.playerbase.event.OnPlayerEventListener
import com.kk.taurus.playerbase.player.IPlayer
import com.kk.taurus.playerbase.receiver.ReceiverGroup
import com.kk.taurus.playerbase.widget.BaseVideoView
import com.videoaggre.zcl.helper.player.DataInter
import com.videoaggre.zcl.helper.player.ReceiverGroupManager
import com.videoaggre.zcl.utils.Logger

open class BasePlayerActivity(layoutResID: Int? = null) : BaseActivity(layoutResID), OnPlayerEventListener {
    protected lateinit var mReceiverGroup: ReceiverGroup
    protected lateinit var mVideoView: BaseVideoView
    private var userPause = false
    private var isLandscape = false
    private var hasStart = false
    fun initVideoView(videoView: BaseVideoView) {
        mVideoView = videoView
        mReceiverGroup = ReceiverGroupManager.getVideoReceiverGroup(this)
        mReceiverGroup.groupValue.putBoolean(DataInter.Key.KEY_CONTROLLER_TOP_ENABLE, true)// 是否显示标题栏
        videoView.setReceiverGroup(mReceiverGroup)
        videoView.setEventHandler(onVideoViewEventHandler)
        videoView.setOnPlayerEventListener(this)
    }

    private val onVideoViewEventHandler: OnVideoViewEventHandler = object : OnVideoViewEventHandler() {
        override fun onAssistHandle(assist: BaseVideoView, eventCode: Int, bundle: Bundle?) {
            super.onAssistHandle(assist, eventCode, bundle)
            when (eventCode) {
                InterEvent.CODE_REQUEST_PAUSE -> userPause = true
                DataInter.Event.EVENT_CODE_REQUEST_BACK -> if (isLandscape) {
                    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                } else {
                    finish()
                }
                DataInter.Event.EVENT_CODE_REQUEST_TOGGLE_SCREEN -> requestedOrientation =
                    if (isLandscape)
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                    else
                        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                DataInter.Event.EVENT_CODE_ERROR_SHOW -> mVideoView.stop()
            }
        }

        override fun requestRetry(videoView: BaseVideoView, bundle: Bundle) {
//            if (PUtil.isTopActivity(this@BaseVideoViewActivity)) {
//                super.requestRetry(videoView, bundle)
//            }
        }
    }

    override fun onPlayerEvent(eventCode: Int, bundle: Bundle?) {
        when (eventCode) {
            OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE -> {
                Logger.e("播放完毕")
            }
        }
    }

    fun startPlay(url: String, title: String) {
        val dataSource = DataSource(url)
        dataSource.title = title
        mVideoView.setDataSource(dataSource)
        mVideoView.start()
    }

    private fun initPlay() {
        if (!hasStart) {
//            val dataSource = DataSource("https://mov.bn.netease.com/open-movie/nos/mp4/2017/05/31/SCKR8V6E9_hd.mp4".videoCacheUrl)
//            dataSource.title = "音乐和艺术如何改变世界"
//            videoView.setDataSource(dataSource)
//            videoView.start()
            hasStart = true
        }
    }

    override fun onResume() {
        super.onResume()
        val state: Int = mVideoView.state
        if (state == IPlayer.STATE_PLAYBACK_COMPLETE) return
        if (mVideoView.isInPlaybackState) {
            if (!userPause) mVideoView.resume()
        } else {
            mVideoView.rePlay(0)
        }
        //initPlay()
    }

    override fun onBackPressed() {
        if (isLandscape) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            return
        }
        super.onBackPressed()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        isLandscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
        mReceiverGroup.groupValue.putBoolean(DataInter.Key.KEY_IS_LANDSCAPE, isLandscape)
    }

    override fun onPause() {
        super.onPause()
        val state: Int = mVideoView.state
        if (state == IPlayer.STATE_PLAYBACK_COMPLETE) return
        if (mVideoView.isInPlaybackState) {
            mVideoView.pause()
        } else {
            mVideoView.stop()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mVideoView.stopPlayback()
    }

}